package cn.edu.zjut.medlinebackend.controller;

import cn.edu.zjut.medlinebackend.model.Post;
import cn.edu.zjut.medlinebackend.model.User;
import cn.edu.zjut.medlinebackend.model.dto.PictureEsDTO;
import cn.edu.zjut.medlinebackend.model.dto.PostEsDTO;
import cn.edu.zjut.medlinebackend.model.dto.PostQuery;
import cn.edu.zjut.medlinebackend.model.vo.PostByTagVO;
import cn.edu.zjut.medlinebackend.model.vo.PostVO;
import cn.edu.zjut.medlinebackend.service.PictureService;
import cn.edu.zjut.medlinebackend.service.PostService;
import cn.edu.zjut.medlinebackend.utils.MyUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/post")
public class PostController {
    @Resource
    private PostService postService;

    @Resource
    private PictureService pictureService;

    /**
     * 接收搜索条件，从数据库中进行搜索
     * @param postQuery 接收的条件
     * @return  返回搜索到的结果
     */
    @PostMapping ("/search")
    public List<Post> doSearch(@RequestBody PostQuery postQuery){
        if(postQuery == null){
            return null;
        }
        System.out.println(postQuery);
        return postService.getBySearchText(postQuery);
    }
    @PostMapping ("/search1")
    public String testSearch(@RequestBody PostQuery postQuery){
        if(postQuery == null){
            return null;
        }
        System.out.println(postQuery);
        return "成功";
    }

    /**
     * 搜索模块，接收前端传来的搜索条件，把查询到的结果返回给前端
     * @param postQuery  接收到的搜索条件
     * @return 整合好的搜索结果
     */
    @PostMapping ("/searchFromEs")
    public Page<PostVO> doSearchEs(@RequestBody PostQuery postQuery){
        if(postQuery == null){
            return null;
        }
        String keyWord = postQuery.getSearchText();
        String UserName = postQuery.getUserName();

        if(keyWord.isEmpty()){
            return null;
        }
        Page<PostEsDTO> postFromEs = postService.getPostFromEs(postQuery);
        // todo 从文件的内容中没有搜索到结果 解决
        // todo 分页  解决
        if(postFromEs.getTotal() == 0){
            List<PictureEsDTO> pictureEsDTOList = pictureService.getAllPicFromEs(keyWord);
            return postService.picEsListToPostVOPage(pictureEsDTOList, postQuery);
        }
        //todo 这里会因为没有对搜索词进行分词，导致文章的内容为空；
        //      有时候也会因为只有标题匹配了，但是内容中不包含搜索词，导致返回的内容为空    已解决
        return postService.getResult(postFromEs, keyWord,UserName);
    }

    /**
     *  用户根据标签进行搜索   标签可以有多个
     * @param postQuery 前端传来的搜索条件
     * @return 封装好的搜索结果
     */
    @PostMapping ("/searchByTags")
    public Page<PostByTagVO> searchByTags(@RequestBody PostQuery postQuery){
        if(postQuery == null){
            return null;
        }

        String s = postQuery.getTags();

        if(s.isEmpty()){
            return null;
        }

        Page<Post> postPage = postService.selectByTags(postQuery);

        return postService.getResultByTag(postPage, postQuery);
    }


    /**
     * 智能问答模块，接收前端传来的问题信息，把生成的回答返回给前端
     * @param question  前端传来的问题信息
     * @return 生成的回答
     */
    @RequestMapping(value="/question/{question}" ,method = RequestMethod.GET)
    public ResponseEntity<Map<String, String>> respone(@PathVariable("question") String question){
        if(question==null){
            return null;
        }
        String answer="对不起，我不理解你的问题";
        Map<String, String> response = new HashMap<>();
        response.put("answer", answer);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    /**
     * 点赞功能，对文章进行点赞
     * @param fileName 要点赞的文章（文件）名
     * @param request  请求体
     * @return  返回是否点赞成功
     */
    @GetMapping("/like")
    public String giveAlike(String fileName, HttpServletRequest request){
        String username = MyUtils.getCookieValue(request, "username");
        if(postService.giveAlike(fileName,username)==1)
            return "点赞成功";
        else return "请先登录";
    }

    /**
     * 点踩功能，对文章进行点踩
     * @param fileName 要点踩的文章（文件）名
     * @param request   请求体
     * @return 返回是否点踩成功
     */
    @GetMapping("/dislike")
    public String giveAdislike(String fileName, HttpServletRequest request){
        String username = MyUtils.getCookieValue(request, "username");
        if(postService.giveAdislike(fileName,username)==1){
            return "点踩成功";
        }
        else return "请先登录";
    }
}
